package easy.二叉树的中序遍历;

import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

public class Solution2 {
    public List<Integer> inorderTraversal(TreeNode root) {
        List<Integer> result = new ArrayList<>();
        Stack<TreeNode> stack = new Stack<>();
        while (root != null || !stack.isEmpty()) {
            while (root != null) {
                stack.push(root);
                root = root.left;
            }
            root = stack.pop();//出站后的结果重新赋值给root
            result.add(root.val);
            root = root.right;
        }
        return result;
    }


    public static void main(String[] args) {
        TreeNode root = new TreeNode(1, null, new TreeNode(2, new TreeNode(3), null));
        Solution2 solution = new Solution2();
        List<Integer> res = solution.inorderTraversal(root);
        res.forEach(i -> System.out.println(i));
    }
}
